package com.yoso.config;

import cn.hutool.core.util.ReflectUtil;
import com.yoso.annotation.CustomProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Field;

/**
 * @author shizejin
 * @date 2022/4/6
 * @description:
 */
@Configuration
public class InitProperties implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Class<?> aClass = bean.getClass();
        CustomProperties annotation = aClass.getAnnotation(CustomProperties.class);
        if (annotation == null) {
            return bean;
        }
//        System.out.println("before");
        Field[] fieldList = aClass.getDeclaredFields();
        for (Field field : fieldList) {
            String name = field.getName();
            if ("name".equals(name)) {
                ReflectUtil.setFieldValue(bean,field,"yoso");
            }
        }
        return bean;
    }

}
